﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Util
{
    public class ResultMsg<T>
    {
        public int Code { get; set; }
        public string Msg { get; set; }
        public int Count { get; set; }
        public T Data { get; set; } // 泛型类型T用于数据  

        // 构造函数，可以用于初始化ResultMsg对象  
        public ResultMsg()
        {
        }

        // 通用设置方法，允许指定所有属性  
        public ResultMsg<T> SetResultMsg(int code = 0, string msg = "", int count = 0, T data = default)
        {
            Code = code;
            Msg = msg;
            Data = data;
            Count = count;
            return this;
        }

        // 成功方法  
        public ResultMsg<T> Success(T data = default, int count = 0)
        {
            Code = 200;
            Msg = "Success";
            Data = data;
            Count = count;
            return this;
        }

        // 错误方法  
        public ResultMsg<T> Error(string msg = "Error")
        {
            Code = 400;
            Msg = msg;
            Data = default; // 对于泛型T，使用default关键字  
            Count = 0;
            return this;
        }

        // 异常方法  
        public ResultMsg<T> Exception()
        {
            Code = 500;
            Msg = "Internal Exception";
            Data = default; // 对于泛型T，使用default关键字  
            Count = 0;
            return this;
        }

        // 糟糕的请求方法  
        public ResultMsg<T> Bad()
        {
            Code = 400;
            Msg = "糟糕的请求";
            Data = default; // 对于泛型T，使用default关键字  
            Count = 0;
            return this;
        }
    }
    // 自定义JSON转换器
    public class CustomJsonConverter<T> : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return serializer.Deserialize(reader);
        }

        public override bool CanConvert(Type objectType)
        {
            return typeof(T) == objectType;
        }
    }
}
